package com.barcap.customui.helper
{
	import com.barcap.customui.vo.Listener;
	import com.barcap.customui.vo.ShortcutKey;
	import com.barcap.events.ShortcutKeyEvent;

	import flash.events.KeyboardEvent;

	import mx.collections.ArrayCollection;


	public class ShortcutManager
	{
		private static var listeners:ArrayCollection = new ArrayCollection();
		public static var popupOpen:Boolean = false;
		public static var enableShortcuts:Boolean = true;

		public static function register(listenerObject:Object, shortcutKey:ShortcutKey, receiverFunction:Function):void
		{
			if (listenerObject && shortcutKey && receiverFunction != null)
			{
				var listener:Listener = new Listener();
				listener.listenerObject = listenerObject;
				listener.receiverFunction = receiverFunction;
				listener.shortcutKey = shortcutKey;

				if (!listeners.contains(listener))
				{
					listeners.addItem(listener);
				}
			}
		}

		public static function remove(listenerObject:Object = null, shortcutKey:ShortcutKey = null):void
		{
			if (listenerObject || shortcutKey)
			{
				for (var i:int; i < listeners.length; i++)
				{
					var listener:Listener = listeners[i] as Listener;

					if (listenerObject && shortcutKey)
					{
						if (listener.listenerObject == listenerObject && listener.shortcutKey == shortcutKey)
						{
							listeners.removeItemAt(i);							
						}
					}
					else if (shortcutKey)
					{
						if (listener.shortcutKey == shortcutKey)
						{
							listeners.removeItemAt(i);
						}
					}
					else if (listenerObject)
					{
						if (listener.listenerObject == listenerObject)
						{
							listeners.removeItemAt(i);
						}
					}
				}
			}
		}

		public static function event(event:KeyboardEvent):void
		{
			if (enableShortcuts)
			{
				var filteredCollection:ArrayCollection = filterCollection(event.altKey, event.ctrlKey, event.shiftKey);
				for each (var listener:Listener in filteredCollection)
				{
					var shortcutKey:ShortcutKey = listener.shortcutKey;

					if (shortcutKey.functionKey)
					{
						if (event.keyCode == shortcutKey.functionKeyCode)
						{
							var shortcutKeyEvent:ShortcutKeyEvent = new ShortcutKeyEvent(ShortcutKeyEvent.SHORTCUTKEY_EVENT, shortcutKey, event);
							listener.receiverFunction(shortcutKeyEvent);
						}
					}
					else if (event.charCode == shortcutKey.uppCharCode || event.charCode == shortcutKey.lowCharCode)
					{
						var shortcutKeyEvent1:ShortcutKeyEvent = new ShortcutKeyEvent(ShortcutKeyEvent.SHORTCUTKEY_EVENT, shortcutKey, event);
						listener.receiverFunction(shortcutKeyEvent1);
					}
				}
			}
		}

		private static function filterCollection(alt:Boolean, ctrl:Boolean, shift:Boolean):ArrayCollection
		{
			var filteredCollection:ArrayCollection = new ArrayCollection();
			for each (var listener:Listener in listeners)
			{
				var shortcutKey:ShortcutKey = listener.shortcutKey;
				if (shortcutKey.altEnabled == alt && shortcutKey.controlEnabled == ctrl && shortcutKey.shiftEnabled == shift && shortcutKey.active)
				{
					if (popupOpen)
					{
						if (!shortcutKey.isPopup)
						{
							filteredCollection.addItem(listener);
						}
					}
					else
					{
						filteredCollection.addItem(listener);
					}
				}
			}
			return filteredCollection;
		}

	}
}